package cac.fenjian.resource.ReturnApplyQuery.rest;

import cac.fenjian.resource.ReturnApplyQuery.service.ReturnApplyService;
import cac.fenjian.resource.WmsReturnApply.dto.WmsReturnApplyDTO;
import com.alibaba.fastjson.JSON;
import com.cac.dme.adapter.rest.msg.PageParameter;
import com.cac.dme.adapter.rest.msg.QueryReqBean;
import com.cac.dme.adapter.rest.msg.QueryRespBean;
import com.cac.dme.adapter.rest.msg.ResponseMsg;
import com.cac.dme.adapter.utils.ContextCommonHolder;
import com.cac.dme.common.utils.JsonHelper;
import com.fasterxml.jackson.core.type.TypeReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import static com.cac.dme.adapter.utils.BusinessUtil.getSortExpColumnName;
@RestController
@RequestMapping("/ReturnApplyQuery")
public class ReturnApplyRest {
    @Autowired
    private ReturnApplyService returnApplyService;
    //零件退货页面条件查询
    @PostMapping("/ParamsByReturnQuery")
    public ResponseMsg<QueryRespBean<WmsReturnApplyDTO>> searchParamsBy(@RequestBody Map map) {
        ResponseMsg<QueryRespBean<WmsReturnApplyDTO>> responseMsg = new ResponseMsg<>();
        //时间范围
        String startTimeParams = JSON.toJSONString(map.get("startTime"));
        String endTimeParams = JSON.toJSONString(map.get("endTime"));
        Date startTime = JSON.parseObject(startTimeParams, Date.class);
        Date endTime = JSON.parseObject(endTimeParams, Date.class);
        //分页
        String param = JSON.toJSONString(map.get("pageParameter"));
        PageParameter pageParameter = JSON.parseObject(param, PageParameter.class);
        //查询条件
        String searchParamsParams = JSON.toJSONString(map.get("searchParams"));
        WmsReturnApplyDTO searchParams = JSON.parseObject(searchParamsParams, WmsReturnApplyDTO.class);

        QueryReqBean<WmsReturnApplyDTO> queryReqBean = new QueryReqBean<>();
        queryReqBean.setSearchParams(searchParams);
        queryReqBean.setPageParameter(pageParameter);

        if (StringUtils.hasText(queryReqBean.getSidx()) && StringUtils.hasText(queryReqBean.getSord())) {
            String sordExp = getSortExpColumnName(queryReqBean.getSidx(), queryReqBean.getSord(), WmsReturnApplyDTO.class);
            if (StringUtils.hasText(sordExp)) {
                queryReqBean.setSortExp(sordExp);
            }
        }
        if (StringUtils.hasText(queryReqBean.getKeyWord())) {
            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            WmsReturnApplyDTO searchKeyWordParam = JsonHelper.getInstance().readValue(queryReqBean.getKeyWord(), dateFormat, new TypeReference<WmsReturnApplyDTO>() {
            });
            queryReqBean.setSearchParams(searchKeyWordParam);
        }

        QueryRespBean<WmsReturnApplyDTO> result = returnApplyService.ReturnApplyQuery(queryReqBean, ContextCommonHolder.getOrgId(), queryReqBean.getSortExp(), startTime, endTime);
        List<WmsReturnApplyDTO> result1 = returnApplyService.ReturnApplyQuery1(searchParams, queryReqBean.getSortExp(), startTime, endTime);
        if(result1.size()!=0){
            responseMsg.setRetCode("200").setResponseBody(result);
        }
        else responseMsg.setResponseBody(result).setRetCode("201").setMessage("未查询到数据");
        return responseMsg;
    }
}
